package com.broada.apm.dao.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.broada.apm.dao.AlertDao;
import com.broada.apm.model.Alert;
import com.broada.apm.model.ConfigItem;
import com.broada.apm.model.PagingList;

public class MysqlAlertDao extends MysqlAbstractDao implements AlertDao {
	
	public static final String ALERT_NAMESPACE = "com.broada.apm.dao.AlertDao.";

	@Override
	public void addAlert(Alert alert) {
		getSqlSession().insert(ALERT_NAMESPACE + "addAlert", alert);
	}

	@Override
	public void updateAlert(Alert alert) {
		getSqlSession().update(ALERT_NAMESPACE + "updateAlert", alert);
	}

	@Override
	public void deleteAlert(Alert alert) {
		getSqlSession().delete(ALERT_NAMESPACE + "deleteAlert", alert);
	}

	@Override
	public PagingList<Alert> getAlert(Alert alert, int pageIndex, int pageSize,
			Date startDate, Date endDate, String sortField, String sortBy) {
		List<Alert> result = null;
		
		PagingList<Alert> page = new PagingList<Alert>();
		page.setStartPage(pageIndex);
		page.setPageSize(pageSize);

		if (StringUtils.isNotBlank(alert.getType())) {
			if ("system".equals(alert.getType())) {
				alert.setType("('rpm', 'lrpm', 'rtt', 'error', 'success', 'apdex')");
			} else if ("business".equals(alert.getType())) {
				alert.setType("('brpm', 'blrpm', 'brtt', 'berror', 'bsuccess', 'bapdex')");
			} else if (alert.getType().equals("rpm")) {
				alert.setType("('rpm', 'lrpm')");
			} else if (alert.getType().equals("brpm")) {
				alert.setType("('brpm', 'blrpm')");
			} else {
				alert.setType("('" + alert.getType() + "')");
			}
		}
		page.getMap().put("alert", alert);
		page.getMap().put("startDate", startDate);
		page.getMap().put("endDate", endDate);
		if (StringUtils.isNotBlank(sortField)) {
			if ("repeat".equals(sortField)) {
				sortField = "repeated";
			}
			if ("desc".equals(sortField)) {
				sortField = "description";
			}
			if ("clearStatus".equals(sortField)) {
				sortField = "clear_status";
			}
			page.getMap().put("sortField", sortField);
			page.getMap().put("sortBy", sortBy);
		} else {
			page.getMap().put("sortField", "lastTime");
			page.getMap().put("sortBy", "DESC");
		}
		
		result = getSqlSession().selectList(ALERT_NAMESPACE + "getAlert",
				page);

		page.setData(result);
		PagingList.pagingHandler(pageIndex, pageSize, (int)page.getTotalRecords(), page);
		return page;
	}

	@Override
	public int getAlertCountBySeverity(int severity) {
		return getSqlSession().selectOne(ALERT_NAMESPACE + "getAlertCountBySeverity", severity);
	}

	@Override
	public Alert getAlertById(String id) {
		if (StringUtils.isBlank(id)) {
			return null;
		}
		return getSqlSession().selectOne(ALERT_NAMESPACE + "getAlertById", id);
	}

	@Override
	public int getServerCountBySeverity(int severity) {
		return getSqlSession().selectOne(ALERT_NAMESPACE + "getServerCountBySeverity", severity);
	}

	@Override
	public List<Alert> getAlertBySeverityAndServiceId(String id, int serverity) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("serviceId", id);
		map.put("serverity", serverity);
		List<Alert> result = getSqlSession().selectList(ALERT_NAMESPACE + "getAlertBySeverityAndServiceId", map);
		
		return result;
	}
	
	@Override
	public void addAlertThreshold(ConfigItem configItem) {
		getSqlSession().insert(ALERT_NAMESPACE + "addAlertThreshold", configItem);
	}

	@Override
	public void updateAlertThreshold(ConfigItem configItem) {
		getSqlSession().update(ALERT_NAMESPACE + "updateAlertThreshold", configItem);
	}

	@Override
	public void deleteAlertThreshold(ConfigItem configItem) {
		getSqlSession().delete(ALERT_NAMESPACE + "deleteAlertThreshold", configItem);
	}

	@Override
	public ConfigItem getAlertThreshold(String key) {
		return getSqlSession().selectOne(ALERT_NAMESPACE + "getAlertThreshold", key);
	}
	
	@Override
	public List<ConfigItem> getAlertThresholdLikeKey(String key) {
		return getSqlSession().selectList(ALERT_NAMESPACE + "getAlertThresholdLikeKey", key);
	}

	@Override
	public int getAlertCountBySeverityCondition(int severity, String appId, String serviceId, Date startTime, Date endTime) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("severity", severity);
		map.put("appId", appId);
		map.put("serviceId", serviceId);
		map.put("startTime", startTime);
		map.put("endTime", endTime);
		return getSqlSession().selectOne(ALERT_NAMESPACE + "getAlertCountBySeverityCondition", map);
	}

	@Override
	public Integer getAppAlertCount(String appId, Date startTime, Date endTime) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("appId", appId);
		map.put("startTime", startTime);
		map.put("endTime", endTime);
		return getSqlSession().selectOne(ALERT_NAMESPACE + "getAppAlertCount", map);
	}

	@Override
	public Map<String, Map<String, Long>> getAppAlertCountByType(String appId, String serviceId, Date startTime, Date endTime) {
		Map<String, Object> map = new HashMap<String, Object>();
//		if ("rpm".equals(type)) {
//			map.put("type", "'rpm' or type = 'lrpm'");
//		} else if ("brpm".equals(type)) {
//			map.put("type", "'brpm' or type = 'blrpm'");
//		} else {
//			map.put("type", "'" + type + "'");
//		}
		map.put("appId", appId);
		map.put("serviceId", serviceId);
		map.put("startTime", startTime);
		map.put("endTime", endTime);
		return getSqlSession().selectMap(ALERT_NAMESPACE + "getAppAlertCountByType", map, "type");
	}

	@Override
	public Map<String, Map<String, Long>> getAlertCountBySeverity(String appId, String serviceId, Date startTime, Date endTime) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("appId", appId);
		map.put("serviceId", serviceId);
		map.put("startTime", startTime);
		map.put("endTime", endTime);
		return getSqlSession().selectMap(ALERT_NAMESPACE + "getAlertCountMapBySeverity", map, "severity");
	}

	@Override
	public Map<String, Map<String, Object>> getAlertByServiceAndSeverity(List<String> servIdList, Date startTime, Date endTime, int severity) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("serviceIds", servIdList.toArray(new String[servIdList.size()]));
		map.put("startTime", startTime);
		map.put("endTime", endTime);
		map.put("severity", severity);
		return getSqlSession().selectMap(ALERT_NAMESPACE + "getAlertByServiceAndSeverity", map, "serviceId");
	}

	@Override
	public Map<String, Map<String, Long>> getAlertCountBySeverity(String groupBy, Date startTime, Date endTime) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("groupBy", groupBy);
		map.put("startTime", startTime);
		map.put("endTime", endTime);
		return getSqlSession().selectMap(ALERT_NAMESPACE + "getAlertCountMapByGroupSeverity", map, "groupBy");
	}

	@Override
	public Map<String, Map<String, Long>> getAlertCountByType(String groupBy, Date startTime, Date endTime) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("groupBy", groupBy);
		map.put("startTime", startTime);
		map.put("endTime", endTime);
		return getSqlSession().selectMap(ALERT_NAMESPACE + "getAlertCountMapByGroupType", map, "groupBy");
	}

}
